import { Context } from 'koa';
import statisticsService from '../services/statistics.service';

/**
 * 统计控制器
 * @description 处理统计相关接口
 */
class StatisticsController {
  /**
   * 获取系统概况统计
   * @param ctx Koa上下文
   */
  public async getDashboardOverview(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      // 并行获取各类统计数据
      const [articleStats, userStats, commentStats] = await Promise.all([
        statisticsService.getArticleStats(period),
        statisticsService.getUserStats(period),
        statisticsService.getCommentStats(period),
      ]);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取系统概况统计成功',
        data: {
          article: articleStats,
          user: userStats,
          comment: commentStats,
        },
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取文章统计数据
   * @param ctx Koa上下文
   */
  public async getArticleStats(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      const stats = await statisticsService.getArticleStats(period);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取文章统计数据成功',
        data: stats,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取用户统计数据
   * @param ctx Koa上下文
   */
  public async getUserStats(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      const stats = await statisticsService.getUserStats(period);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取用户统计数据成功',
        data: stats,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取评论统计数据
   * @param ctx Koa上下文
   */
  public async getCommentStats(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      const stats = await statisticsService.getCommentStats(period);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取评论统计数据成功',
        data: stats,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取分类统计数据
   * @param ctx Koa上下文
   */
  public async getCategoryStats(ctx: Context): Promise<void> {
    try {
      const stats = await statisticsService.getCategoryStats();

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取分类统计数据成功',
        data: stats,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取标签统计数据
   * @param ctx Koa上下文
   */
  public async getTagStats(ctx: Context): Promise<void> {
    try {
      const limit = parseInt(ctx.query.limit as string) || 20;

      const stats = await statisticsService.getTagStats(limit);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取标签统计数据成功',
        data: stats,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取热门文章
   * @param ctx Koa上下文
   */
  public async getPopularArticles(ctx: Context): Promise<void> {
    try {
      const limit = parseInt(ctx.query.limit as string) || 10;
      const days = parseInt(ctx.query.days as string) || 30;

      const articles = await statisticsService.getPopularArticles(limit, days);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取热门文章成功',
        data: articles,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取文章趋势数据
   * @param ctx Koa上下文
   */
  public async getArticleTrend(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      const trend = await statisticsService.getArticleTrend(period);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取文章趋势数据成功',
        data: trend,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }

  /**
   * 获取用户增长趋势数据
   * @param ctx Koa上下文
   */
  public async getUserGrowthTrend(ctx: Context): Promise<void> {
    try {
      const period = ctx.query.period as string || 'month';

      const trend = await statisticsService.getUserGrowthTrend(period);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: '获取用户增长趋势数据成功',
        data: trend,
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : '服务器内部错误',
      };
    }
  }
}

export default new StatisticsController();
